#include "Stdafx.h"	

#include "EndGameDb.h"

namespace EndGameDB
{

int64 BinomCoef[MAX_FIELD][MAX_FIELD];
int64 BinomCoefTrans[MAX_FIELD][MAX_FIELD];


int64 ComputeBinomCoef(int n, int k)
{
	int64 result = 1;
	int64 i;

	i=k;
	while(i)
		{
		result *= (n-i+1);
		i--;
		}

	i=k;
	while(i)
		{
		result /=i;
		i--;
		}

	return result;
}


void InitBinomCoef()
{
	for(int i=0; i<MAX_FIELD; i++)
	{
		for(int j=1; j<=i; j++)
		{
			BinomCoef[i][j] = ComputeBinomCoef(i,j);
		}
		
		// define choosing 0 for simplicity 
		BinomCoef[i][0] = 1;
	} // for


	// choosing n from 0: bicoef = 0
	for(int i=1; i < MAX_FIELD; i++)
		BinomCoef[0][i]=0;

	// transposed bicoef array is faster sometimes.
	for(int i=0; i < MAX_FIELD; i++)
	{
		for(int j=0; j < MAX_FIELD; j++)
			BinomCoefTrans[i][j] = BinomCoef[j][i];
	} // for

}



}
